/*
 *  WilNoteObserver.cpp
 *  Wilson
 *
 *  Created by Sebastian Orfino on 1/18/12.
 *  Copyright 2012 lbs3. All rights reserved.
 *
 */


#include "VCPlugInHeaders.h"
#include "WilID.h"
#include "CObserver.h"
#include "GenericID.h"
#include "PageItemScrapID.h"
#include "StandOffID.h"
#include "ICommand.h"
#include "IWilNoteData.h"
#include "UIDList.h"

#include "csxs_facade.h"
#include "xmp_facade.h"
#include "eliminar_marcador_response.h"

using namespace xmp;
using namespace std;

class WilNoteObserver : public CObserver {

public:
	
	WilNoteObserver(IPMUnknown * boss) : CObserver(boss,IID_IWILNOTEOBSERVER) {}
	virtual ~WilNoteObserver() { }
	
	
	/** Handle broadcasts on protocols of interest.
	 @param theChange specifies the class ID of the change to the subject. Frequently this is a command ID.
	 @param theSubject points to the ISubject interface for the subject that has changed.
	 @param protocol specifies the ID of the changed interface on the subject boss.
	 @param changedBy points to additional data about the change. Often this pointer indicates the class ID of the command that has caused the change.
	 */
	virtual void Update(const ClassID& theChange, ISubject* theSubject, const PMIID& protocol, void* changedBy);
	
private:
	void HandlePageItemDeleted(void* changedBy);
};

CREATE_PMINTERFACE(WilNoteObserver, kWilNoteObserverImpl)

void WilNoteObserver::Update(const ClassID& theChange, ISubject* theSubject, const PMIID& protocol, void* changedBy)
{
	//> kDeletePageItemCmdBoss @ kDocBoss (IID_IHIERARCHY_DOCUMENT)
	
	do
	{
		ICommand* iCommand = (ICommand*)changedBy;
		const UIDList itemList = iCommand->GetItemListReference();
		
		if (protocol == IID_IHIERARCHY_DOCUMENT)
		{
			if ((theChange == kDeletePageItemCmdBoss) ||
				(theChange == kDeleteStandOffItemCmdBoss) ||
				(theChange == kDeleteInsetPageItemCmdBoss))
			{
				if (itemList.Length() && 
					(iCommand->GetCommandState() == ICommand::kNotDone))
				{
					// We only do this when we are pre-notified that a page item is about to be deleted
					// so we need to tidy up any data this plug-in may have associated with it.
					HandlePageItemDeleted(changedBy);
				}
			}
		}
		
	} while (kFalse);

}


void WilNoteObserver::HandlePageItemDeleted(void* changedBy)
{
	do
	{
		ICommand* cmd = (ICommand*)changedBy;
		const UIDList* selectUIDList = cmd->GetItemList();
		
		if (selectUIDList->Length() < 1)
		{
			ASSERT_FAIL("invalid page item");
			break;
		}
		
		int32 item = selectUIDList->Length();
		UIDList myUIDList = *selectUIDList;
		while (item-- > 0)
		{
			InterfacePtr<IWilNoteData> marcador(myUIDList.GetRef(item), UseDefaultIID());
			if (marcador)
			{
				// como elimine el marcador, tengo que actualizar la metadata
				wilson_metadata metadata(::GetUIDRef(this));
				vector<info_nota_t> notas = metadata.listar_notas();
				const string id = marcador->uid().str();
				
				for (vector<info_nota_t>::iterator it = notas.begin();
					 it != notas.end();
					 ++it) {
					
					if (it->id == id) {
						it->tiene_marcador = kFalse;
						metadata.modificar_nota(*it);
						break;
					}
				}
				
				// y luego avisar que el usuario elimino el marcador.
				eventos::eliminar_marcador_response evt;
				evt.nota().id = id;
				evt.resultado(true);
				wilson::despachar(evt);
			}
		}
	} while (false);
}
